/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.db.impl;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Person;
import com.yunhoo.base.db.dao.PersonDao;
import com.yunhoo.sys.db.util.DbTool;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.RemoteSeverParam;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class PersonDaoImpl extends SqlMapDaoTemplate implements PersonDao {

    private static SqlMapClient sqlmap;
    private String nameSpace = "person.";
    private Object cpsn_number;

    public PersonDaoImpl(DaoManager daoManager) {
        super(daoManager);
        sqlmap = DbTool.getSqlMapClient();
    }

    @Override
    public void insert(Person org) throws SQLException {
        sqlmap.insert(nameSpace + "insert", org);
    }

    @Override
    public void update(Person org) throws SQLException {
        System.out.println(nameSpace + "update");
        sqlmap.update(nameSpace + "update", org);
    }

    @Override
    public void updatePWD(Person org) throws SQLException {
        System.out.println(nameSpace + "updatePWD");
        sqlmap.update(nameSpace + "updatePWD", org);
    }

    @Override
    public void delete(Person org) throws SQLException {
        System.out.println(nameSpace + "delete");
        sqlmap.delete(nameSpace + "delete", org);
    }

    @Override
    public void deleteById(String cpsn_number) throws SQLException {
        System.out.println(nameSpace + "deleteById");
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("cpsn_number", cpsn_number);
        sqlmap.delete(nameSpace + "deleteById", map);
    }

    @Override
    public Person queryById(String cpsn_number) throws SQLException {
        System.out.println(nameSpace + "queryById");
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        map.put("cpsn_number", cpsn_number);
        return (Person) sqlmap.queryForObject(nameSpace + "queryById", map);
    }

    @Override
    public List<Person> queryAll() throws SQLException {
        System.out.println(nameSpace + "queryAll");
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", LoginParam.getIyear());
        return sqlmap.queryForList(nameSpace + "queryAll", map);
    }

    @Override
    public List<Person> queryListByDept(Person param) throws SQLException {
        System.out.println(nameSpace + "queryListByDept");
        return sqlmap.queryForList(nameSpace + "queryListByDept", param);
    }

    @Override
    public List<Person> queryListByRoleId(String roleId) {
        System.out.println(nameSpace + "queryListByRoleId");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("iyear", LoginParam.getIyear());
            map.put("roleId", roleId);
            return sqlmap.queryForList(nameSpace + "queryListByRoleId", map);
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public Person queryByIdAndPwd(Person per) throws SQLException {
        System.out.println(nameSpace + "queryByIdAndPwd");
        return (Person) sqlmap.queryForObject(nameSpace + "queryByIdAndPwd", per);
    }

    @Override
    public boolean idIsRepeat_add(int id) {
        System.out.println(nameSpace + "idIsRepeate_add");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("id", id);
            List li = sqlmap.queryForList(nameSpace + "idIsRepeate_add", map);
            return li != null && !li.isEmpty();
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean idIsRepeat_update(Person per) {
        System.out.println(nameSpace + "idIsRepeate_update");
        try {
            List li = sqlmap.queryForList(nameSpace + "idIsRepeate_update", per);
            return li != null && !li.isEmpty();
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean numberIsRepeat(String cpsn_number) {
        System.out.println(nameSpace + "numberIsRepeate");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("cpsn_number", cpsn_number);
            return sqlmap.queryForObject(nameSpace + "numberIsRepeate", map) != null;
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean nameIsRepeate_add(String cpsn_name) {
        System.out.println(nameSpace + "nameIsRepeate_add");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("cpsn_name", cpsn_name);
            List li = sqlmap.queryForList(nameSpace + "nameIsRepeate_add", map);
            return li != null && !li.isEmpty();
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean nameIsRepeate_update(Person per) {
        System.out.println(nameSpace + "nameIsRepeate_update");
        try {
            List li = sqlmap.queryForList(nameSpace + "nameIsRepeate_update", per);
            return li != null && !li.isEmpty();
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public Integer maxCode(String deptCode) {
        System.out.println(nameSpace + "maxCode");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("deptCode", LoginParam.getDept().getCode());
            return (Integer) sqlmap.queryForObject(nameSpace + "maxCode", map);
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }

    @Override
    public Integer maxId() {
        System.out.println(nameSpace + "maxId");
        try {
            Map map = new HashMap();
            map.put("orgCode", LoginParam.getOrg().getCode());
            map.put("deptCode", LoginParam.getDept().getCode());
            return (Integer) sqlmap.queryForObject(nameSpace + "maxId", map);
        } catch (SQLException ex) {
            Logger.getLogger(PersonDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }
}
